
"""
过滤器函数,为点击排行榜选择样式
"""
import functools

from flask import session, g

from info.models import User


def do_class_index(data):
    if data == 1:
        return "first"
    elif data == 2:
        return "second"
    elif data == 3:
        return "third"
    else:
        return data


def funcTotal(data):
    return len(data)


# 定义判断是否登录的装饰器
def check_islogin(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # 从session取出id
        user_id = session.get("user_id")
        user = None
        # 判断是否登录
        if user_id:
            # 如果登录则查询出对应的user
            user = User.query.get(user_id)
        # 把user对象存到g变量
        g.user = user
        return func(*args, **kwargs)
    return wrapper